args a1 a2 a3 a4 

local lab Binary
preserve

* setup
local ipw=1

local lwage  lwage_I23  lwage_I33  lwage_I42  lwage_I46  lwage_I50  lwage_I55
local AE     AE_I23     AE_I33     AE_I42     AE_I46     AE_I50     AE_I55
local lpay   lpay_I23   lpay_I33   lpay_I42   lpay_I46   lpay_I50   lpay_I55  
local lhours  lhours_I23 lhours_I33 lhours_I42 lhours_I46 lhours_I50 lhours_I55  

local O  lwage lpay lhours AE
local O1 lwage lpay lhours
local O2 AE


* calculate ipw weights (including self employed)
foreach o of local O {

	foreach y of local `o' {
		if `ipw'==1{
			ge io=`y'!=.
			qui logit io $HDC_0
			drop io
			predict p_`y', pr
		}

		if `ipw'==0{
			ge  p_`y'=1
		}
	
	}	
}

* calculate ipw weights (excluding self employed)
foreach o of local O1 {
	foreach y of local `o' {
	
		if `ipw'==1{
			local a=substr("`y'",-3,.) /* find instrument and age*/
			ge io=`y'!=. if SE_`a'==0
			qui logit io $HDC_0
			drop io
			predict pse_`y', pr
			replace pse_`y'=1 if MissingMA_0==1 & pse_`y'==. & p_`y'!=. /* fix for 100% successful prediction */
		}

		if `ipw'==0{
			ge  pse_`y'=1
		}
	
	}	
}

* reshape to long dataset
reshape long lwage_I lpay_I lhours_I AE_I SE_I p_lwage_I p_lpay_I p_lhours_I p_AE_I pse_lwage_I pse_lpay_I pse_lhours_I, i(NCDSID) j(age)
su p_*


ge HCreatHCog=BCogAbility*Bcreativityt_psd_7

* estimation including self employed
foreach o of local O1 {
	rifhdreg `o'  $Controls BCogAbility Bcreativityt_psd_7 HCreatHCog  [pweight=1/p_`o'], rif(mean) cluster(NCDSID)
	
	mat b = e(b)
	mat b = b'
	svmat b
	qui su b1 if b1!=0
	mat define p= r(N)
	drop b1
	qui estadd scalar Par=p[1,1]
	qui estadd scalar Ni=e(N_clust) 
	qui estadd local mdv "-"
	est sto es_`o'
}


foreach o of local O2 {

	rifhdreg `o'  $Controls BCogAbility Bcreativityt_psd_7 HCreatHCog  [pweight=1/p_`o'], rif(mean) cluster(NCDSID)
	
	mat b = e(b)
	mat b = b'
	svmat b
	qui su b1 if b1!=0
	mat define p= r(N)
	drop b1
	qui estadd scalar Par=p[1,1]
	qui estadd scalar Ni=e(N_clust) 
	qui estadd scalar mdv=e(rifmean)
	est sto es_`o'

}



* count number of variables
local count0: word count `O'
local count=`count0'+1

* without p-values
******************************************************************************************


local text0 "prehead( & \multicolumn{3}{c}{Individuals in work} &    \\ \vspace{-.25cm} \\ \cline{2-4} \\ \vspace{-.65cm}\\ ) "
local text  `text0' prefoot( \cline{2-`count'}  \vspace{-.3cm}\\ )  posthead(\vspace{-.3cm}\\ \cline{2-`count'} )  postfoot(\vspace{-.3cm} \\  ) 

* Stats
local stP "stats( N mdv, fmt( %11.0gc %8.2f)  labels("Individual-year obs."  "Mean dep. var."))" 

* all individuals
esttab es* using results/Tables/R_OLS_`lab'.tex, ///
replace `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
keep(BCogAbility Bcreativityt_psd_7 HCreatHCog) refcat(Bcreativityt_psd_7 ""  " " "", nolabel) ///
order(Bcreativityt_psd_7 BCogAbility HCreatHCog) varlabels(BCogAbility "High cognitive ability"  Bcreativityt_psd_7 " High creativity" HCreatHCog "High creat. $\times$ High cog. ab.") ///
cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) ///
mtitles("`a1'" "`a2'" "`a3'" "`a4'" "`a5'" "`a6'" "`a7'" "`a8'") frag `stP'


est drop _all
restore
